package com.eam.eventhandler;

import com.eam.data.EamFacilities;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.client.kernel.event.*;
import javax.enterprise.event.Observes;

public class FacilityTypeEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EamFacilities.ENTITY_NAME) };

	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}

	public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
		if (!isValidEvent(event)) {
			return;
		}

		this.checkeData(event);
	}

	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		this.checkeData(event);
	}
	
	private void checkeData(EntityPersistenceEvent event) {
//		EamFacilities emesFacilities = (EamFacilities) event.getTargetInstance();
//		final Property fTypeProperty = entities[0].getProperty(EamFacilities.PROPERTY_TYPE);
//		// 获取tabId，用来判断新增的哪种类型
//		String tabId = SessionInfo.getProcessId();
//		
//		//判断IP地址是否正确
//		if(emesFacilities.getIp1() != null && !CommonUtils.isboolIp(emesFacilities.getIp1())){
//			String language = OBContext.getOBContext().getLanguage().getLanguage();
//			ConnectionProvider conn = new DalConnectionProvider(false);
//			throw new OBException(Utility.messageBD(conn, "输入的IP地址_1不正确，请仔细检查。", language));
//		}
//		if(emesFacilities.getIp2() != null && !CommonUtils.isboolIp(emesFacilities.getIp2())){
//			String language = OBContext.getOBContext().getLanguage().getLanguage();
//			ConnectionProvider conn = new DalConnectionProvider(false);
//			throw new OBException(Utility.messageBD(conn, "输入的IP地址_2不正确，请仔细检查。", language));
//		}
//
//		if (emesFacilities.getIp1() != null && emesFacilities.getIp2() != null
//						&& emesFacilities.getIp1().equalsIgnoreCase(emesFacilities.getIp2())) {
//			String language = OBContext.getOBContext().getLanguage().getLanguage();
//			ConnectionProvider conn = new DalConnectionProvider(false);
//			throw new OBException(Utility.messageBD(conn, "填入的IP地址相同，请仔细检查。", language));
//		}
//
//		//从设备设备/工模tab创建的数据
//		if (tabId.equalsIgnoreCase("7069FFC8A6E044E8B3AB0F3295AA6655")) {
//			Long use_time = emesFacilities.getLimitTime();
//			BigDecimal use_date = emesFacilities.getLimitDate();
//			Long limit_time = emesFacilities.getLimitTimeTo();
//			BigDecimal limit_date = emesFacilities.getLimitDateTo();
//
//			if (use_time == null && use_date == null) {
//				String language = OBContext.getOBContext().getLanguage().getLanguage();
//				ConnectionProvider conn = new DalConnectionProvider(false);
//				throw new OBException(Utility.messageBD(conn, "设备的限制使用次数或寿命周期都不能为空，请仔细检查。", language));
//			} else if (use_time != null) {
//				if (limit_time == null) {
//					String language = OBContext.getOBContext().getLanguage().getLanguage();
//					ConnectionProvider conn = new DalConnectionProvider(false);
//					throw new OBException(Utility.messageBD(conn, "设备限制使用次数需设置预警值，请仔细检查。", language));
//				}
//			} else {
//				if (limit_date == null) {
//					String language = OBContext.getOBContext().getLanguage().getLanguage();
//					ConnectionProvider conn = new DalConnectionProvider(false);
//					throw new OBException(Utility.messageBD(conn, "设备寿命周期需设置寿命预警值，请仔细检查。", language));
//				}
//			}
//		}
//
//		if ("85D1FE3F03F94F58B6EB675EBDA1FDBA".equalsIgnoreCase(tabId)) {
//			event.setCurrentState(fTypeProperty, "10"); // 设备
//		} else if ("17E05359D8B045CEB2C784A07C24C0C6".equalsIgnoreCase(tabId)) {
//			if (emesFacilities.getModelType().equalsIgnoreCase("10")) {
//				event.setCurrentState(fTypeProperty, "30"); // 工装
//			} else {
//				event.setCurrentState(fTypeProperty, "20"); // 模具
//			}
//		}
	}

	public void onDelete(@Observes EntityDeleteEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
	}
}
